﻿using OAWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OAWeb.Models
{
    public partial class BLL : Controller
    {
        //计算排名
        public string[,] s  { get; set; }
        public int stuLength { get; set; }

        public int e { get; set; }
        public double[] ssum { get; set; }
        public double avg { get; set; }
        public void ScoresRand(List<StudentScore> scores, Student[] students, Course[] courses, Grade[] grades, Grade[] grades1)
        {

            double[,] Scores = new double[students.Length, courses.Length];
            //这里开始
            for (int i = 0; i < students.Length; i++)
            {
                for (int j = 0; j < courses.Length; j++)
                {
                    //判断学生是否录入考试
                    for (int k = 0; k < grades.Length; k++)
                    {
                        if (grades[k].StudentID == students[i].ID)
                        {
                            Scores[i, j] = scores.Single(g => g.StdNumber == students[i].ID && g.CourseName == courses[j].Subject).Score;
                            //break;
                        }

                    }

                }

            }
            // ViewBag.scores = Scores;
            string[,] s = new string[students.Length, 7];
            double[] ssum = new double[courses.Length];
            double sum = 0;
            double avg = 0;

            int stuLength = 0;

            for (int c = 0; c < courses.Length; c++)
            {
                for (int d = 0; d < grades1.Length; d++)
                {
                    sum += Scores[d, c];
                }
                ssum[c] = sum / grades1.Length;
            }


            int e = 2;
            for (int i = 0; i < students.Length; i++)
            {
                e = 2;
                for (int k = 0; k < grades.Length; k++)
                {
                    if (grades[k].StudentID == students[i].ID)
                    {

                        double scoretotal = 0;


                        for (int j = 0; j < courses.Length; j++)
                        {


                            scoretotal += Scores[i, j];

                        }




                        s[i, 0] = students[i].Number;
                        s[i, 1] = students[i].Name;
                        for (int i1 = 0; i1 < courses.Length; i1++)
                        {
                            s[i, 2 + i1] = Scores[i, i1].ToString();
                            e++;
                        }

                        s[i, e] = scoretotal.ToString();
                        s[i, e + 1] = students[i].ID.ToString();

                        stuLength++;
                    }

                }

            }

            avg = sum / stuLength;

            for (int c = 0; c < stuLength - 1; c++)
            {
                for (int a = 0; a < stuLength - 1; a++)
                {
                    if (int.Parse(s[a, e]) < int.Parse(s[a + 1, e]))
                    {
                        for (int b = 0; b < courses.Length + 4; b++)
                        {
                            string[,] s1 = new string[courses.Length, courses.Length + 4];
                            s1[a, b] = s[a, b];
                            s[a, b] = s[a + 1, b];
                            s[a + 1, b] = s1[a, b];
                        }
                    }
                }

            }
            /*ViewBag.s = s;
            ViewBag.stuLength = stuLength;
            ViewBag.e = e;
            ViewBag.ssum = ssum;
            ViewBag.avg = avg;*/
            this.s = s;
            this.stuLength = stuLength;
            this.e = e;
            this.ssum = ssum;
            this.avg = avg;
           
        }
    }
}